from db_module import *
from common import *
from google.appengine.ext import webapp

class Regbook(webapp.RequestHandler):
    def get(self):
        nickname = ""
        reg = Register.gql("where username = :1", get_username())
        if reg:
            nickname = reg.get().nickname
        self.response.out.write("""<a href="/">Back to home</a><br />Please enter your nickname<form action="/reg" method="post">
                <div><input name="nickname" value="%s"/></div>
                <div><input type="submit" name="submit" value="Register"></div>
              </form>
            </body>
          </html>""" % nickname)
    def post(self):
        reg = Register.gql("where username = :1", get_username())
        if reg:
            register = reg.get()
            register.nickname = self.request.get('nickname')
            register.save()
        else:
            register = Register()
            register.username = get_username()
            register.nickname = self.request.get('nickname')
            register.put()
        self.redirect('/')